package com.lbmb.mdps.workbench.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.domain.SupplierReceive;
import com.lbmb.mdps.commons.domain.SupplierVo;
import com.lbmb.mdps.commons.exception.MedicineTypeException;
import com.lbmb.mdps.commons.exception.SupplierException;
import com.lbmb.mdps.commons.utils.DateTimeUtil;
import com.lbmb.mdps.commons.utils.SnowFlakeIdUtil;
import com.lbmb.mdps.workbench.domain.Supplier;
import com.lbmb.mdps.workbench.service.SupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 林文塨
 * @date 2022-05-30 23:11
 * description
 */
@CrossOrigin
@RestController
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @PostMapping( "/workbench/supplier/supplierPageList.do")
    public ReturnObjectVO supplierPageList(@RequestBody SupplierReceive supplierReceive) throws SupplierException {
        /*
         * 分页获取供应商列表
         * @author 林文塨
         * @date 2022/5/30 23:15
         * @param [supplierReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        supplierReceive.setSkipCount((supplierReceive.getPageNo()-1)*supplierReceive.getPageSize());

        List<Supplier> supplierList = supplierService.supplierPageList(supplierReceive);

        rov.setOthers(supplierList);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/supplier/getSupplierTotal.do")
    public ReturnObjectVO getSupplierTotal(@RequestBody SupplierReceive supplierReceive) throws SupplierException {
        /*
         * 获取供应商分页总条数功能
         * @author 林文塨
         * @date 2022/5/30 10:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Integer total = supplierService.getSupplierTotal(supplierReceive);
        rov.setNumber(total);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/supplier/addSupplier.do")
    public ReturnObjectVO addSupplier(@RequestBody Supplier supplier) throws SupplierException {
        /*
         * 添加供应商
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        supplier.setCreateTime(DateTimeUtil.getSysTime());
        supplier.setCreateBy(supplier.getCurrentUserId());
        supplier.setDeleteFlag("0");
        supplier.setId(SnowFlakeIdUtil.generateId());

        supplierService.addSupplier(supplier);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/supplier/editSupplier.do")
    public ReturnObjectVO editSupplier(@RequestBody Supplier supplier) throws SupplierException {
        /*
         * 添加供应商
         * @author 林文塨
         * @date 2022/5/30 12:38
         * @param [employeeReceive]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        supplier.setEditTime(DateTimeUtil.getSysTime());
        supplier.setEditBy(supplier.getCurrentUserId());

        supplierService.editSupplier(supplier);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @PostMapping( "/workbench/supplier/deleteSupplier.do")
    public ReturnObjectVO deleteEmployee(@RequestBody String [] deleteSupplierIds) throws SupplierException {
        /*
         * 删除供应商
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        supplierService.deleteSupplier(deleteSupplierIds);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }

    @GetMapping( "/workbench/supplier/getSupplierData.do")
    public ReturnObjectVO getMedicineTypeData() {
        /*
         * 获取全部供应商
         * @author 林文塨
         * @date 2022/5/30 16:17
         * @param [employee]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        List<SupplierVo> supplierData = supplierService.getAllSupplier();
        /*ServletContext servletContext = session.getServletContext();
        Object supplierData = servletContext.getAttribute(Constant.SUPPLIER_DATA);*/

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(supplierData);

        return rov;
    }
}
